﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;

namespace FileOperatorWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateTreeView();
            this.Title = "文件浏览器（by littlewyy）";
        }
        private void PopulateTreeView()
        {
            string rootPath = "C:\\";
            DirectoryItem rootItem = new DirectoryItem(rootPath, false); // 不是文件
            rootItem.LoadSubDirectories(); // 求子目录名字，并存储
            folderTreeView.ItemsSource = new List<DirectoryItem> { rootItem }; // 绑定数据
            folderListView.ItemsSource = new List<DirectoryItem> { rootItem }; // 绑定数据
            currentPath.Text = rootPath;
        }
        private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            DirectoryItem directoryItem = (DirectoryItem)item.DataContext;
            if (directoryItem.IsFile == false) // 如果是目录
            {
               directoryItem.LoadSubDirectories(); // 该节点被点击了才加载子项目录
               item.ItemsSource = directoryItem.SubDirectories;// 更新左侧树形视图
               if (folderListView.ItemsSource != directoryItem.SubDirectories) // 更新右侧列表视图
               {
                    folderListView.ItemsSource = directoryItem.SubDirectories;
               }
                // 更新路径显示
               currentPath.Text = "当前路径为：" + directoryItem.Name;
            }
            e.Handled = true; // 事件已解决，无需向上回传
        }
        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DirectoryItem selectedItem = (DirectoryItem)folderListView.SelectedItem;
            // 执行逻辑
            if (selectedItem.IsFile == true) // 如果是文件，则打开
            {
                string name = selectedItem.Name;
                int l = name.Length;
                if (l > 4)
                {
                    string tmp = name.Substring(l - 4, 4);
                    if (tmp == ".exe")
                    {
                        Process p = Process.Start(name);
                        p.WaitForExit();
                    }
                    else
                    {
                        if (tmp == ".txt")
                        {
                            Process.Start("notepad.exe", name);
                        }
                    }
                }
            }            
        }
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}